<?php 
    include 'top.php'; 
    include '../util.php';
?>
<script src="uploadify/swfobject.js"  language="javascript" ></script>
<script src="uploadify/jquery.uploadify.v2.1.4.min.js"  language="javascript" ></script>
<link href="uploadify/uploadify.css" rel="stylesheet" type="text/css" />

<?php
    if ($_REQUEST['action'] == 'add') {
        $fileName = make_safe($_REQUEST['images']);
        $uploadName = date('Y-m-d-h-i-s.jpg');
        rename(realpath('../upfiles/tmp/'.$fileName), realpath('../upfiles').'/'.$uploadName);

        $sql = sprintf("INSERT INTO banner(title, images, sort_index, enabled, add_time) VALUES('%s', '%s', %d, 1, now())", make_safe($_REQUEST['title']), "upfiles/$uploadName", make_safe($_REQUEST['sort_index']));
        $result = mysql_query($sql);
        if (mysql_affected_rows() > 0) {
            Util::jsAlert('新增首页图片成功', 'banner.php');
        }else{
            Util::jsAlert(mysql_error(), 'banner.php');
        }
    }
?>

<script type="text/javascript">
$(function() {
    $('input#uploadify').uploadify({
        'uploader': 'uploadify/uploadify.swf',
        'cancelImg': 'uploadify/cancel.png',
        'script': 'upload.php',
        'folder': 'files',
        'buttonText':'Choose',
        'multi': false,
        'fileExt': '*.jpg;*.jpeg;*.gif;*.png',
        'fileDesc': 'Image Files',
        'auto': true,
        'queueSizeLimit': 1,
        'scriptData': {'<?php echo session_name();?>':'<?php echo session_id();?>','name': $(this).attr('name'),'responsetype':'sizeinfo'},
        'displayData': 'speed',
        'onComplete': function(event,queueID,fileObj,response,data) {
            var responseObj = eval("("+response+")");
            var fileName = responseObj.name;
            $(':input[name="images"]').val(fileName);
            $('#bannerImg').show().attr('src','../upfiles/tmp/' + fileName);
        },
        'onError':function(event,queueId,fileObj,errorObj) {
            alert(errorObj);
        }
    });

    $('form[name="banner_edit"]').submit(function(evt) {
        if ( isNaN(parseInt($(this).find('input[name="sort_index"]').val())) ){
            alert('圖片顯示順序必須為數字，請重新修改');
            $(this).find('input[name="sort_index"]').val('').focus();
            return false;
        }
    });
});
</script>

    <div id="top-3">
        <div id="m-text-1">
            <ul>
                <li>系統管理功能</li>
           
                <li><a href="admin_manage.php">管理管理員</a></li>
                <li><a href="banner.php">管理首页图片</a></li>
            </ul>
        </div>
	 
	 
        <div id="m-text-2_banner">
          <ul>
            <li>首頁圖片管理 -  新增</li>
          </ul>
        </div>
		
		<div id="m-text-2">
            <form name="banner_edit" action="<?php echo $_SERVER['PHP_SELF']; ?>?action=add" method="post">
                <table cellpadding='0' cellspacing='0' border='0'>
                    <tr>
                        <td width="200">圖片:</td>
                        <td>
                            <input type="hidden" value="" name="images" />
                            <img src="" height="110" id="bannerImg" style="display: none; margin-bottom: 10px" />
                            <input type="file" value="" id="uploadify"/>
                        </td>
                    </tr>
                    <tr>
                        <td>圖片標題:</td>
                        <td>
                            <input type="textfield" name="title" value="" />
                        </td>
                    </tr>
                    <tr>
                        <td>顯示順序:</td>
                        <td>
                            <input type="textfield" name="sort_index" value="0" />
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <input type="submit" value="新增" />
                        </td>
                    </tr>
                <table>
            </form>
        </div>

    </div>
  </div>
</div>
</div>
<div id="botton"><div id="copyright"><table border="0" cellpadding="0" cellspacing="0" class="copy">
  <tr>
    <td>© COPYRIGHTS 2010 INC. APARTMENT STUDIO All rights reserved.</td>
  </tr>
</table>
</div></div>
</body>
</html>
